import java.util.Scanner;

/**
 * 题目1：十进制转N进制程序
 * 使用栈实现十进制数转换为2-9进制
 */
public class DecimalConverter {
    /**
     * 将十进制整数转换为N进制
     * 
     * @param decimal 十进制整数
     * @param base    目标进制（2-9）
     * @return N进制字符串表示
     */
    public static String convert(int decimal, int base) {
        // 验证进制范围
        if (base < 2 || base > 9) {
            throw new IllegalArgumentException("进制必须在2-9之间");
        }

        // 处理特殊情况
        if (decimal == 0) {
            return "0";
        }

        // 处理负数
        boolean isNegative = decimal < 0;
        if (isNegative) {
            decimal = -decimal;
        }

        // 使用栈存储余数
        StackInterface<Integer> stack = new ArrayStack<Integer>(100);

        // 不断除以base，将余数入栈
        while (decimal > 0) {
            int remainder = decimal % base;
            stack.push(remainder);
            decimal = decimal / base;
        }

        // 将栈中的余数依次出栈，组成结果字符串
        StringBuilder result = new StringBuilder();
        if (isNegative) {
            result.append("-");
        }
        while (!stack.isEmpty()) {
            result.append(stack.pop());
        }

        return result.toString();
    }

    /**
     * 显示转换过程的详细信息
     * 
     * @param decimal 十进制整数
     * @param base    目标进制
     */
    public static void showConversionProcess(int decimal, int base) {
        System.out.println("\n========== 转换过程 ==========");
        System.out.println("十进制数: " + decimal);
        System.out.println("目标进制: " + base + "进制");

        if (decimal == 0) {
            System.out.println("结果: 0");
            return;
        }

        boolean isNegative = decimal < 0;
        int num = isNegative ? -decimal : decimal;

        StackInterface<Integer> stack = new ArrayStack<Integer>(100);
        int step = 1;

        System.out.println("\n计算步骤：");
        while (num > 0) {
            int remainder = num % base;
            int quotient = num / base;
            System.out.printf("步骤%d: %d ÷ %d = %d ... %d (余数%d入栈)\n",
                    step++, num, base, quotient, remainder, remainder);
            stack.push(remainder);
            num = quotient;
        }

        System.out.println("\n出栈过程（从栈顶到栈底）：");
        StringBuilder result = new StringBuilder();
        if (isNegative) {
            result.append("-");
        }
        int stepNum = 1;
        while (!stack.isEmpty()) {
            int digit = stack.pop();
            result.append(digit);
            System.out.printf("步骤%d: 出栈 %d，当前结果: %s\n", stepNum++, digit,
                    isNegative ? "-" + result.substring(1) : result.toString());
        }

        System.out.println("\n最终结果: " + (isNegative ? "-" : "") + result.toString());
        System.out.println("=============================");
    }

    /**
     * 主程序
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("========== 十进制转N进制程序 ==========");
        System.out.println("说明：可以将十进制整数转换为2-9进制");

        while (true) {
            try {
                System.out.print("\n请输入十进制整数（输入-999退出）: ");
                int decimal = scanner.nextInt();

                if (decimal == -999) {
                    System.out.println("程序退出");
                    break;
                }

                System.out.print("请输入目标进制（2-9）: ");
                int base = scanner.nextInt();

                if (base < 2 || base > 9) {
                    System.out.println("错误：进制必须在2-9之间！");
                    continue;
                }

                // 显示转换过程
                showConversionProcess(decimal, base);

                // 直接转换并显示结果
                String result = convert(decimal, base);
                System.out.println("\n转换结果: (" + decimal + ")₁₀ = (" + result + ")" + getBaseSuffix(base));

            } catch (Exception e) {
                System.out.println("输入错误：" + e.getMessage());
                scanner.nextLine(); // 清除输入缓冲区
            }
        }

        scanner.close();
    }

    /**
     * 获取进制后缀显示
     */
    private static String getBaseSuffix(int base) {
        String[] suffixes = { "", "", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉" };
        return suffixes[base] + "（" + base + "进制）";
    }
}
